
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js?v=23"></script><!--终端命令结果查询-->


<script>
    /*
	修改代码：
	1.新定义变量TerminalCommmand，为命令支持变量
	2.重定义id：
	原：null		现：broadcastVideoTermialCommandMsg
	原：null		现：btnStartTalkBroad
	原：		现：
	*/

	var tc2BroadcastVideoTerminal = new TerminalCommand2();
     var timerName = "broadcastVideoTerminalTimer";
	///////////////////////////////////////////////////////////////

    $().ready(function () {
        tc2BroadcastVideoTerminal.messageDiv = $("#broadcastVideoTermialCommandMsg");
        tc2BroadcastVideoTerminal.timerName = timerName;
        tc2BroadcastVideoTerminal.commandButton = $("#btnStartTalkBroad");
        $("#entityFormBroad").validate(); //初始化验证信息

        $("#btnStopTalkBroad").click(function () {
            stopTalk();
    });

    var defaultChannelId = "1";//默认选中
    $("#channelBroad").fillVehicleChannels({vehicleId:"${vehicleId}",selectedValue:defaultChannelId});
	  Utility.ajaxSubmitForm("entityFormBroad", {
						 success:function(responseText)
						 {
							   var result = responseText;
								if (result.success) {
									 var commandId = result.data; //下发成功后，获取到命令Id
									 tc2BroadcastVideoTerminal.startQueryResult(commandId,null,onTalkCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果
								}
								else {
									$("#broadcastVideoTermialCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
									//停止所有的在$('body')上定时器  
									$('body').stopTime (timerName);  
								  }
						 }
	 });
});

    function  startListen() {
        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = $("#channelBroad").val();
        var ret = window.parent.MyVideoPlayer.startListen(vehicleId, simNo, channel);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }

    }

    function stopListen()
    {
        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = $("#channelBroad").val();
        var ret = window.parent.MyVideoPlayer.stopListen(simNo);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            //this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }
    }


	function  startTalk() {
	    var vehicleId = ${vehicleId};
	    var simNo = "${simNo}";
        var channel = $("#channelBroad").val();

        var audioSampleRate = 44;
        var encodeQuality = 8;
        var ret = window.parent.MyVideoPlayer.startTalkback(vehicleId, "broadcast", channel,audioSampleRate,encodeQuality);
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
			alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }

    }

    function onStopTalkback()
	{
	    //stopListen();

        var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = $("#channelBroad").val();
        var ret = window.parent.MyVideoPlayer.stopTalkback("broadcast");
        if (ret == 0) {
            //$("input[name='listing']").attr("disabled", true);
            //this.isListening = true;
            //$("#listenTip").html(lang.requiring);
            //$("#btnListen").html(lang.stop);
        } else if(ret == -1) {//视频插件未开启
            //$.dialog.tips(parent.lang.vedio_undefined, 1);
            alert("lisetn error");
        }else{
            //一般都不会到此接口
            //除非视频插件没有初始化完成
        }
	}

    function stopTalk()
	{
	    var vehicleId = ${vehicleId};
        var simNo = "${simNo}";
        var channel = 1;
	    var url = "<%=ApplicationPath%>/videoCommand/stopBroadcast.action";
	    var allSelectedVehicle = $("#allSelectedVehicle").attr("checked") ? "on" : "off";
	    var param = {vehicleId:vehicleId, simNo:simNo,channelId:channel,allSelectedVehicle:allSelectedVehicle};
	    $.getJSON(url, param, function(result)
		{
		    var message = result.success ? "停止成功": "停止失败";
		    $("#broadcastVideoTermialCommandMsg").html(message);
		    if(result.success)
			{
                var commandId = result.data; //下发成功后，获取到命令Id
                tc2BroadcastVideoTerminal.commandButton = $("#btnStopTalkBroad");
                tc2BroadcastVideoTerminal.startQueryResult(commandId,null,onStopTalkback);//命令下发成功,根据命令id,开始尝试获取检索结果
			}

		})
	}

    function onTalkCommandSuccess()
	{
	    startTalk();
        //startListen();

	}


    /**
     * 当easyui的dialog 被关闭的时候，会自动调用此方法，停止监听
     * @returns {boolean}
     */
    function onWindowBeforeClose()
    {
        stopTalk();
        stopListen();
        console.log("监听窗口被关闭，自动停止对讲");
        return true;
    }


</script>
 <BODY>
	<form id="entityFormBroad" name="entityFormBroad" 
			action='<%=ApplicationPath%>/videoCommand/broadcast.action' method="POST">
				
        <input type="hidden"  name="vehicleId"  id="vehicleId" value="${vehicleId}"/>
		<input type="hidden"  name="simNo"  id="simNo" value="${simNo}"/>
  <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="2" style="font-weight: bold; background: #EFEFEF;" height="25">广播信息
						<span style="color:red;background:blue;">${message}</span>
						</td>
					</tr>

					<tr>
						<td align="right">通道
						:</td>
						<td>
							<select id="channelBroad" name="channelBroad">
							</select>
						</td>
					</tr>
					<tr>


						<td colspan=2 align="center">
							<button id="btnStartTalkBroad" type="submit" class="button green medium sendjson" ><i class="fa fa-microphone" style="margin-right:3px;"></i> 开始广播</button>

							<button id="btnStopTalkBroad" type="button" class="button red medium " ><i class="fa fa-microphone-slash" style="margin-right:3px;"></i> 停止广播</button>


							<input type="checkbox" name="allSelectedVehicle" id="allSelectedVehicle" value="on"/>给所有选中的车辆下发
						   <span id="broadcastVideoTermialCommandMsg" class="commandMsg"></span></td>
						
					</tr>

				
					
				</tbody></table>
 </BODY>
</HTML>
